<?php
    namespace app\admin\model;
    use think\Model;
    use think\request;
    use think\Db;

    
    class Classes extends Model
    {

        public function class_list(){
            $map =[];
            //$map['Singer_Id'] = array('eq',2);
            $request = Request::instance();
            $data = $this->where($map)->order('Class_id asc')->select();
            return $data;
        }

        public function classno_list(){
            $map =[];
            //$map['Singer_Id'] = array('eq',2);
            $request = Request::instance();
            $classdata = $this->where($map)->field('Class_no')->select();
            return $classdata;
        }

        
        public function change_classno_list(){
            $map =[];
            //$map['Singer_Id'] = array('eq',2);
            $request = Request::instance();
            $classnodata = $this->where($map)->field('Class_no')->select();
            return $classnodata;
        }

        public function newclassno_list(){
            $map =[];
            //$map['Singer_Id'] = array('eq',2);
            $request = Request::instance();
            $newclassdata = $this->where($map)->field('Class_no')->select();
            return $newclassdata;
        }

        public function insert($data){
            for($i=0;$i<count($data['Coursename']);$i++)
                $array[$i]=$data['Coursename'][$i];
                    $str=implode(',',$array);

            $classno = $data['Classno'];
            
            // var_dump($str);

            $result = Db::table('classes')->insert([
                'Class_no'=>$classno,
                'Class_course'=>$str,
            ]);

            if ($result) {
                return 1; //新添成功
            } else {
                return 2; //新添失败
            }
        }

        public function change($data){

            $classno = $data['c_no'];
            $cid = $data['c_id'];
            
            for($i=0;$i<count($data['Coursename']);$i++)
                $array[$i]=$data['Coursename'][$i];
                    $str=implode(',',$array);
            
            $result = Db::table('classes')->where('Class_id', $cid)->Update([
                    'Class_no'=>$classno,
                    'Class_course'=>$str,
                ]);
            //返回控制器
            if ($result) {
                return 1; //更新成功返回1
            } else {
                return 2; //更新失败返回2
            }
        }


    }
?>